package br.com.honorato.util;

import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;

import br.com.honorato.bean.AppScopedBean;

public class FacesUtil
{
	public static void showSucessMessage(String summary, String message, boolean keepAlive)
	{
		showMessage(FacesMessage.SEVERITY_INFO, summary, message, keepAlive);
	}

	public static void showAlertMessage(String summary, String message, boolean keepAlive)
	{
		showMessage(FacesMessage.SEVERITY_WARN, summary, message, keepAlive);
	}

	public static void showErrorMessage(String summary, String message, boolean keepAlive)
	{
		showMessage(FacesMessage.SEVERITY_ERROR, summary, message, keepAlive);
	}

	public static void showFatalMessage(String summary, String message, boolean keepAlive)
	{
		showMessage(FacesMessage.SEVERITY_FATAL, summary, message, keepAlive);
	}

	private static void showMessage(FacesMessage.Severity severity, String summary, String message, boolean keepAlive)
	{
		if (null == summary)
		{
			summary = "";
		}

		if (keepAlive)
		{
			FacesContext.getCurrentInstance().getExternalContext().getFlash().setKeepMessages(true);
		}

		FacesMessage facesMessage = new FacesMessage(severity, summary, message);
		FacesContext.getCurrentInstance().addMessage(null, facesMessage);
	}

	/**
	 * Metodo que retorna um objeto Flash para permitir que sejam armazenados
	 * valores entre diversas p�ginas.
	 * 
	 * @return
	 */
	public static Flash getFlash()
	{
		return FacesContext.getCurrentInstance().getExternalContext().getFlash();
	}

	public static AppScopedBean getAppScopedBean()
	{
		AppScopedBean appScopedBean = null;
		Map<String, Object> mapApp = FacesContext.getCurrentInstance().getExternalContext().getApplicationMap();

		if (mapApp.containsKey("appScopedBean"))
		{
			appScopedBean = (AppScopedBean) mapApp.get("appScopedBean");
			if (appScopedBean != null)
			{
				if (appScopedBean.getUserAdminDefault() != null)
				{
					appScopedBean.getUserAdminDefault().getLogin();
				}
			}
		}
		return appScopedBean;
	}

	public static String getLoginAdminDefault()
	{
		AppScopedBean appScopedBean = FacesUtil.getAppScopedBean();
		if (appScopedBean != null)
		{
			if (appScopedBean.getUserAdminDefault() != null)
			{
				return appScopedBean.getUserAdminDefault().getLogin();
			}
		}
		return null;
	}
}